unit uCreateContract;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, OracleData, Oracle, Buttons;

type
  TfmCreateContract = class(TForm)
    Label1: TLabel;
    edClientName: TEdit;
    Button1: TButton;
    Label2: TLabel;
    cbContractType: TComboBox;
    odsCreateContract: TOracleDataSet;
    Label3: TLabel;
    cbContractStatus: TComboBox;
    Label4: TLabel;
    edCreateAction: TEdit;
    btNewContractCreateAction: TButton;
    oqCreateNewContract: TOracleQuery;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btNewContractCreateActionClick(Sender: TObject);
  private
    { Private declarations }
    procedure ClearFields;
  public
    { Public declarations }
    clientId : integer;
    actionId : integer;
    contractTypeId : integer;
    contractStatusId : integer;

    function Execute : boolean;
  end;

var
  fmCreateContract: TfmCreateContract;

implementation

uses uClientSearch, uMain  , uNewContractCreateAction;

{$R *.dfm}

procedure TfmCreateContract.Button1Click(Sender: TObject);
var s : string;
begin
  if fmClientSearch.Execute(clientId, s) then
  begin
    edClientName.Text := s;
  end;

  BitBtn1.Enabled := (clientId > 0) and (actionId > 0);
end;

procedure TfmCreateContract.FormShow(Sender: TObject);
begin
  cbContractType.Items.Clear;
  cbContractStatus.Items.Clear;

  odsCreateContract.Close;
  odsCreateContract.SQL.Text := 'select crtp_id as id, name from contract_types';
  odsCreateContract.Open;

  odsCreateContract.First;

  while not (odsCreateContract.eof) do
  begin
    cbContractType.Items.AddObject(odsCreateContract.FieldByName('NAME').AsString, TObject(odsCreateContract.FieldByName('ID').AsInteger));

    odsCreateContract.Next;
  end;

  odsCreateContract.Close;
  odsCreateContract.SQL.Text := 'select crst_id as id, name from contract_statuses where code = ''new''';
  odsCreateContract.Open;

  odsCreateContract.First;

  while not (odsCreateContract.eof) do
  begin
    cbContractStatus.Items.AddObject(odsCreateContract.FieldByName('NAME').AsString, TObject(odsCreateContract.FieldByName('ID').AsInteger));

    odsCreateContract.Next;
  end;

  cbContractStatus.ItemIndex := 0;
  cbContractStatus.Enabled := false;

end;

procedure TfmCreateContract.btNewContractCreateActionClick(
  Sender: TObject);
begin
  if fmNewContractCreateAction.Execute then
  begin
    oqCreateNewContract.close;

    oqCreateNewContract.DeclareVariable('RESULT', otInteger);

    oqCreateNewContract.SQL.Text := 'begin '+
                                    ':RESULT := pkg_contract_management.create_new_action( '+
                                    'i_action_date => '''+fmNewContractCreateAction.ActionDate+''', i_catp_id => '+IntToStr(fmNewContractCreateAction.ActionTypeId)+'); end;';

    oqCreateNewContract.Execute;

    actionId := oqCreateNewContract.GetVariable('RESULT');

    edCreateAction.Text := fmNewContractCreateAction.ActionName;
  end;

  BitBtn1.Enabled := (clientId > 0) and (actionId > 0);
end;

procedure TfmCreateContract.ClearFields;
begin
  edClientName.Text := '';
  edCreateAction.Text := '';
  cbContractType.ItemIndex := -1;
  cbContractStatus.ItemIndex := -1;
end;

function TfmCreateContract.Execute: boolean;
begin
  ClearFields;

  result := false;

  if ShowModal = mrOk then
  begin
    contractTypeId := integer(cbContractType.Items.Objects[cbContractType.ItemIndex]);
    contractStatusId := integer(cbContractStatus.Items.Objects[cbContractStatus.ItemIndex]);

    result := true;
  end;
end;

end.
